﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoVanHocTapData.Interface;
using CoVanHocTapData.Entities;

namespace CoVanHocTapData.Concretes
{
    public class MonHocDangKyRepository:IMonHocDangKyRepository
    {
        #region repos
        ISinhVienRepository svRepos = new SinhVienRepository();
        IChuongTrinhDaoTaoRepository ctdtRepos = new ChuongTrinhDaoTaoRepository();
        #endregion

        #region constructor
        DangKyPhuEntities entities;
        public MonHocDangKyRepository()
        {
            entities = new DangKyPhuEntities();
        }
        #endregion

        /// <summary>
        /// lay ds mh du kien mo theo nganh cua sv vs dk: sv chua dky o hky do va cac mon hoc nam trog ctdt cua nganh
        /// </summary>
        /// <param name="MSSV"></param>
        /// <param name="HocKyID"></param>
        /// <param name="MonHocIDList">ds mh trong ctdt</param>
        /// <returns></returns>
        public MonHocDangKy LayMonHocDuKienMo(string MSSV, int HocKyID, List<string> MonHocIDList)
        {
            var svDangKy = from m in entities.SinhVienMonDuKienMoes
                           where m.MSSV.Equals(MSSV)
                           select m.MonDuKienMoID;
            var svDangKyMHDuKien = (from m in entities.SinhVienMonDuKienMoes
                                       join n in entities.MonHocs on m.MonDuKienMo.MonHocID equals n.MonHocID
                           where m.MSSV.Equals(MSSV)
                           select n);
            var monHocDuKienCTDT = from m in entities.MonDuKienMoes
                   join n in entities.MonHocs on m.MonHocID equals n.MonHocID
                   where m.HocKyID.Value == HocKyID
                   && !svDangKy.Contains(m.MonDuKienMoID)
                   && MonHocIDList.Contains(m.MonHocID)
                   orderby n.MonHocID
                   select n;
            return new MonHocDangKy()
            {
                MonHocCTDT = monHocDuKienCTDT.ToList(),
                MonHocSVDangKy = svDangKyMHDuKien.ToList()
            };
        }

        public MonHocDangKy LayMonHocDangKyKHHT(string MSSV, int HocKyID)
        {
            SinhVien sv = svRepos.LaySVDangNhap(MSSV);
            //chuong trinh dao tao
            List<string> ctdt = ctdtRepos.LayMonHocChuongTrinhDaoTao(sv.Lop.NganhID, sv.Lop.NienKhoaID.Value, sv.Lop.HeDaoTaoID).ToList();

            //var MHDuKienMoList = LayMonHocDuKienMo(MSSV, HocKyID);
            return LayMonHocDuKienMo(MSSV, HocKyID, ctdt);

        }



        
    }
}
